function minOperations(nums) {
  const n = nums.length;
  // 检查数组中是否有1
  let countOnes = nums.filter((x) => x === 1).length;
  if (countOnes > 0) {
    return n - countOnes;
  }

  // 寻找最短的连续子数组，其GCD为1
  let minLen = Infinity;
  for (let i = 0; i < n; i++) {
    let currentGcd = nums[i];
    for (let j = i; j < n; j++) {
      currentGcd = gcd(currentGcd, nums[j]);
      if (currentGcd === 1) {
        const len = j - i + 1;
        if (len < minLen) {
          minLen = len;
        }
        break; // 继续扩大j只会增加长度，无需再检查
      }
    }
  }

  // 如果找不到这样的子数组，返回-1
  if (minLen === Infinity) {
    return -1;
  }

  // 计算总操作次数
  return minLen - 1 + (n - 1);
}

// 计算两个数的最大公约数
function gcd(a, b) {
  while (b !== 0) {
    [a, b] = [b, a % b];
  }
  return a;
}
